# -*- coding:utf-8 -*-
from rest_framework import serializers

from apps.goods.models import Goods
from apps.order.models import OrderGoods, Order
from shop.settings import IMAGE_URL


class OrderGoodsSerializer(serializers.ModelSerializer):
    class Meta:
        model = OrderGoods
        fields = '__all__'


class OrderSerializer(serializers.ModelSerializer):
    class Meta:
        model = Order
        fields = '__all__'


class OrderMangyGoodsSerializer(serializers.Serializer):
    trade_no = serializers.CharField()
    order_amount = serializers.DecimalField(max_digits=10, decimal_places=2)
    address_id = serializers.IntegerField()
    pay_status = serializers.CharField()
    pay_time = serializers.DateTimeField()
    ali_trade_no = serializers.CharField()
    is_delete = serializers.IntegerField()
    mobile = serializers.CharField()
    create_time = serializers.DateTimeField(format='%Y-%m-%d %H:%M:%S')

    # 关键是下面的内容
    order_info = serializers.SerializerMethodField()

    def get_order_info(self, obj):
        # print(obj)
        ser = OrderGoodsSerializer(OrderGoods.objects.filter(trade_no=obj.trade_no).all(), many=True).data
        # print(ser)
        for i in ser:
            # print(i.get('sku_id'))
            goods_data = Goods.objects.filter(sku_id=i.get('sku_id')).first()
            i["price"] = goods_data.p_price
            i["image"] = IMAGE_URL + goods_data.image
            i["name"] = goods_data.name
            i["shop_name"] = goods_data.shop_name
        return ser
